package com.haixee.parkdemo.custom.controller;

import com.haixee.parkdemo.common.context.WebContextHolder;
import com.haixee.parkdemo.common.response.ResResult;
import com.haixee.parkdemo.common.response.ResUtil;
import com.haixee.parkdemo.custom.entity.vo.CustomVO;
import com.haixee.parkdemo.custom.model.Custom;
import com.haixee.parkdemo.custom.service.CustomService;
import com.haixee.parkdemo.data_acl.annotation.DataAclSetting;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/")
public class CustomController {

    @Autowired
    private CustomService customService;

    @PostMapping("/add")
    public ResResult add(@RequestBody Custom custom) {

        customService.addCustom(custom.getName(), custom.getPhone(), custom.getCompany(), custom.getAddress(), WebContextHolder.getUid());
        return ResUtil.success();
    }

    @PostMapping("/update")
    @DataAclSetting(voClass = CustomVO.class)
    public ResResult update(@RequestBody CustomVO custom) {

        customService.updateCustom(custom.getId(), custom.getName(), custom.getPhone(), custom.getCompany(), custom.getAddress(), WebContextHolder.getUid());
        return ResUtil.success();
    }

    @PostMapping("/assign")
    public ResResult assign(@RequestBody Custom custom) {

        customService.assign(custom.getId(), custom.getOptUid());
        return ResUtil.success();
    }

    @GetMapping("/list")
    @DataAclSetting(voClass = CustomVO.class)
    public ResResult getList() {
        List<Custom> customList = customService.getCustomList(WebContextHolder.getUid());
        List<CustomVO> voList = new ArrayList<>(customList.size());
        for (Custom custom : customList) {
            CustomVO vo = new CustomVO();
            BeanUtils.copyProperties(custom, vo);
            vo.setOptJob(custom.getOptJob());
            voList.add(vo);
        }

        return ResUtil.success(voList);
    }
}
